#include "swf.h"
#include "DefineFont2.h"
using namespace swfstream;
void DefineFont2::print() {
	//	puts("||||||||||||||||||||||||||||||||||");
	//	exit(11);
	printf("define_font2::\n");
	//printf("FontID:%u,\n",(uint)FontID);
	printf("FontID:%u,\n", (uint) CharacterID);
}

DefineFont2* DefineFont2::from_file(FILE* f, int tag_size) {
	int size_read = 0;
	DefineFont2 *r = new DefineFont2;
	//	printf("define_font2::");
	//fread(&r->FontID,2,1,f);
	fread(&r->CharacterID, 2, 1, f);
	size_read += 2;
	//	printf("FontID:%u,",(uint)r->FontID);
	byte tmp = getc(f);
	size_read += 1;
	r->FontFlagsHasLayout = ((tmp & 0x80) != 0);
	r->FontFlagsShiftJIS = ((tmp & 0x40) != 0);
	r->FontFlagsSmallText = ((tmp & 0x20) != 0);
	r->FontFlagsANSI = ((tmp & 0x10) != 0);
	r->FontFlagsWideOffsets = ((tmp & 0x08) != 0);
	r->FontFlagsWideCodes = ((tmp & 0x04) != 0);
	r->FontFlagsItalic = ((tmp & 0x02) != 0);
	r->FontFlagsBold = ((tmp & 0x01) != 0);
	//printf("FontFlagsHasLayout:%u,FontFlagsShiftJIS:%u\nFontFlagsSmallText:%u,FontFlagsANSI:%u\nFontFlagsWideOffsets:%u,FontFlagsWideCodes:%u\nFontFlagsItalic:%u,FontFlagsBold:%u\n",(uint)r->FontFlagsHasLayout,(uint)r->FontFlagsShiftJIS,(uint)r->FontFlagsSmallText,(uint)r->FontFlagsANSI,(uint)r->FontFlagsWideOffsets,(uint)r->FontFlagsWideCodes,(uint)r->FontFlagsItalic,(uint)r->FontFlagsBold);
	r->LanguageCode = getc(f);
	size_read += 1;
	//printf("LanguageCode:%u,",(uint)r->LanguageCode);
	r->FontNameLen = getc(f);
	size_read += 1;
	//printf("FontNameLen:%u,",(uint)r->FontNameLen);
	r->FontName = new byte[r->FontNameLen];
	//	r.FontName[r.FontNameLen]=0;
	fread(r->FontName, r->FontNameLen, 1, f);
	size_read += r->FontNameLen;
	//printf("FontName:%s\n",r->FontName);
	fread(&r->NumGlyphs, 2, 1, f);
	size_read += 2;
	//printf("NumGlyphs:%u,",(uint)r->NumGlyphs);
	if (r->FontFlagsWideOffsets) {
		r->OffsetTable = new uint32[r->NumGlyphs];
		fread(r->OffsetTable, 4, r->NumGlyphs, f);
		size_read += (4 * r->NumGlyphs);
		uint32 tmp;
		fread(&tmp, sizeof(tmp), 1, f);
		size_read += sizeof(tmp);
		r->CodeTableOffset = tmp;
	} else {
		r->OffsetTable = new uint16[r->NumGlyphs];
		fread(r->OffsetTable, 2, r->NumGlyphs, f);
		size_read += (2 * r->NumGlyphs);
		uint16 tmp;
		fread(&tmp, sizeof(tmp), 1, f);
		size_read += sizeof(tmp);
		r->CodeTableOffset = tmp;
	}
	//printf("CodeTableOffset:%u,",(uint)r->CodeTableOffset);
	size_t size_skip; //skip the SHAPETable
	if (r->FontFlagsWideOffsets) {
		size_skip = r->CodeTableOffset - 4 * (r->NumGlyphs + 1);
	} else {
		size_skip = r->CodeTableOffset - 2 * (r->NumGlyphs + 1);
	}
	while (size_skip--) {
		getc(f);
		size_read += 1;
	}
	if (r->FontFlagsWideOffsets) {
		r->CodeTable = new uint16[r->NumGlyphs];
		fread(r->CodeTable, 2, r->NumGlyphs, f);
		size_read += 2 * r->NumGlyphs;
		//FILE* tf=fopen("code.tmp","wb");
		//fwrite(r->CodeTable,2,r->NumGlyphs,tf);
		//fclose(tf);
	} else {
		r->OffsetTable = new uint8[r->NumGlyphs];
		fread(r->CodeTable, 1, r->NumGlyphs, f);
		size_read += r->NumGlyphs;
	}
	if (r->FontFlagsHasLayout) {
		fread(&r->FontAscent, 2, 1, f);
		fread(&r->FontDescent, 2, 1, f);
		fread(&r->FontLeading, 2, 1, f);
		r->FontAdvanceTable = new int16[r->NumGlyphs];
		fread(r->FontAdvanceTable, 2, r->NumGlyphs, f);
		size_read += (6 + 2 * r->NumGlyphs);
		//puts("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
	}
	//skip left part of the tag
	byte tmpbuf[10000];
	//	printf("\nsizes:0x%X,0x%X\n",(uint)r.header.length,(uint)size_read);
	//	fread(tmpbuf,r.header.length-size_read,1,f);
	//printf("\nsizes:0x%X,0x%X\n",(uint)tag_size,(uint)size_read);
	fread(tmpbuf, tag_size - size_read, 1, f);
	return r;
}

